<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                template="../../template/tagedu.xhtml">

    <ui:define name="title">
        <title><h:outputText value="TAG - Edição Unidade"/></title>
        <f:metadata>
            <f:viewParam name="unity" value="#{unityEdit.unityID}"/>
            <f:viewParam name="type" value="#{unityEdit.organizationType}"/>
        </f:metadata>
    </ui:define>

    <ui:define name="content">
        <f:event type="preRenderComponent" listener="#{unityEdit.initialize()}"/>
        <h:outputText styleClass="unityName" value="Edição de #{unityEdit.unity.name}"/>
        <p:messages id="msg"/>
        <p:tabView styleClass="unityRegistrationView"  id="unityEditView">
            <p:tab title="Dados Principais">
                <h:form id="maindEditForm">
                    <p:panel>
                        <h:panelGrid columns="2" styleClass="panelGrids">
                            <h:outputLabel rendered="#{unityEdit.unitiesFather.size() != 1 and unityEdit.unity.unityFather != null}" for="unityFather" value="#{unityEdit.organizationFather}:"/>
                            <h:selectOneMenu rendered="#{unityEdit.unitiesFather.size() != 1 and unityEdit.unity.unityFather != null}" id="unityFather" value="#{unityEdit.unityFatherID}">
                                <f:selectItems value="#{unityEdit.unitiesFather}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="name" value="Nome:"/>
                            <p:inputText styleClass="name" id="name" value="#{unityEdit.unity.name}" required="true" requiredMessage="'Nome' é obrigatório"/>

                            <h:outputLabel for="period" value="Período:"/>
                            <h:selectOneMenu id="period" value="#{unityEdit.periodID}">
                                <f:selectItems value="#{unityEdit.periodList}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="capacity" value="Capacidade:"/>
                            <p:inputText id="capacity" value="#{unityEdit.unity.capacity}"/>

                            <h:outputLabel for="activated" value="Ativação:"/>
                            <p:inputMask id="activated" mask="99/99/9999" value="#{unityEdit.unity.activated.time}"
                                         required="true" requiredMessage="'Ativação' é obrigatório">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </p:inputMask>

                        </h:panelGrid>
                        <h:panelGrid columns="2" id="deactivatedGroup">
                            <h:outputLabel for="deactivated" value="Desativação:" rendered="#{not unityEdit.deactivatedIsNull}"/>
                            <p:inputMask id="deactivated" mask="99/99/9999" value="#{unityEdit.unity.deactivated.time}" 
                                         rendered="#{not unityEdit.deactivatedIsNull}"
                                         required="#{not unityEdit.deactivatedIsNull}" requiredMessage="'Desativação' é obrigatório">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </p:inputMask>

                        </h:panelGrid>
                        <h:outputLabel value="Sem data de desativação:" for="deactivatedIsNull"/>
                        <h:selectBooleanCheckbox id="deactivatedIsNull" value="#{unityEdit.deactivatedIsNull}">
                            <f:ajax execute="deactivatedIsNull" render="deactivatedGroup"/>
                        </h:selectBooleanCheckbox>
                    </p:panel>
                    <p:commandButton action="#{unityEdit.edit()}" value="Editar" update=":msg"/>
                </h:form>
            </p:tab>
            <p:tab title="Dados Jurídicos" rendered="#{unityEdit.needJuridic}">
                <h:form id="juridicEditForm">
                    <p:panel styleClass="juridicTab">
                        <h:panelGrid  columns="2">
                            <h:outputLabel for="companyName" value="Nome Jurídico:"/>
                            <p:inputText id="companyName" value="#{unityEdit.juridic.companyName}" 
                                         required="true" requiredMessage="'Nome Jurídico' é obrigatório" styleClass="name"/>

                            <h:outputLabel for="fancyName" value="Nome Fantasia:"/>
                            <p:inputText id="fancyName" value="#{unityEdit.juridic.fancyName}" 
                                         required="true" requiredMessage="'Nome Fantasia' é obrigatório" styleClass="name"/>

                            <h:outputLabel for="birthday" value="Data de Fundação:"/>
                            <p:inputMask id="birthday" mask="99/99/9999" value="#{unityEdit.juridic.birthday.time}" 
                                         required="true" requiredMessage="'Data de Fundação' é obrigatório">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </p:inputMask>

                            <h:outputLabel for="phone" value="Telefone:"/>
                            <p:inputMask id="phone" mask="(99)9999-9999" value="#{unityEdit.juridic.phone}"/>
                            <h:message styleClass="message" for="phone"/>
                        </h:panelGrid>  
                        <br/>
                        <h:outputLabel styleClass="specificData" rendered="#{not empty unityEdit.additionalInfoList}" value="Dados Específicos:"/>
                        <ui:repeat value="#{unityEdit.additionalInfoList}" var="additionalInfo">
                            <h:panelGrid columns="3">
                                <h:outputText value="#{additionalInfo.additionalInfoType.acronym}: "/>
                                <p:inputMask id="addInfoMask" mask="#{additionalInfo.additionalInfoType.mask}" 
                                             value="#{additionalInfo.value}" required="#{additionalInfo.additionalInfoType.compulsory}" 
                                             requiredMessage="'#{additionalInfo.additionalInfoType.acronym}' é obrigatório"/>
                                <h:message styleClass="message" for="addInfoMask"/>
                            </h:panelGrid>
                        </ui:repeat>
                    </p:panel>
                    <p:commandButton action="#{unityEdit.edit()}" value="Editar" />
                </h:form>
            </p:tab>
            <p:tab title="Endereço" rendered="#{unityEdit.needJuridic}">
                <h:form id="addresEditForm">
                    <p:panel id="addressPanel" styleClass="addressPanel">
                        <p:message for="cep"/>
                        <h:outputLabel value="CEP" for="cep"/>
                        <p:inputMask id="cep" value="#{unityEdit.cep}" mask="99999999" required="true" requiredMessage="'CEP' é obrigatório"/>
                        <p:commandButton onstart="statusDialog.show();" onsuccess="statusDialog.hide();" value="Buscar" action="#{unityEdit.searchCEP()}" update="zipcode" process="@this, cep"/>
                        <br/>
                        <h:panelGroup id="zipcode">
                            <h:outputLabel value="Estado:" for="state"/>
                            <p:inputText id="state" value="#{unityEdit.zipcode.city.state.name}" required="true" readonly="true"/>
                            <br/>
                            <h:outputLabel value="Cidade:" for="city"/>
                            <p:inputText id="city" value="#{unityEdit.zipcode.city.name}" required="true" readonly="true"/>
                            <br/><br/>
                        </h:panelGroup>
                        <h:panelGroup rendered="#{unityEdit.showAddress}">
                            <h:outputLabel value="Bairro:" for="neighborhood"/>
                            <p:autoComplete id="neighborhood" value="#{unityEdit.neighborhood}"
                                            completeMethod="#{unityEdit.neighborhoodComplete}"
                                            required="true" requiredMessage="'Bairro' é campo obrigatório"
                                            minQueryLength="3"/>
                            <br/>
                            <h:outputLabel value="Logradouro:" for="street"/>
                            <p:autoComplete id="street" value="#{unityEdit.street}" 
                                            completeMethod="#{unityEdit.streetComplete}" 
                                            required="true" requiredMessage="'Logradouro' é campo obrigatório"
                                            minQueryLength="3"/>
                            <br/><br/>
                            <h:outputLabel value="Número:" for="number"/>
                            <p:inputText id="number" value="#{unityEdit.address.number}"/>
                            <br/>
                            <h:outputLabel value="Complemento" for="complement"/>
                            <p:inputText id="complement" value="#{unityEdit.address.complement}"/>                            
                        </h:panelGroup>
                    </p:panel>
                    <p:commandButton action="#{unityEdit.edit()}" value="Editar" />
                </h:form>
            </p:tab>
            <p:tab title="Atores">
                <h:form id="actorsForm">
                    <p:panel id="actorsPanel">
                        <p:dataTable id="unityEditTable" value="#{unityEdit.actors}" var="actor"
                                     paginator="true" rows="10" rowsPerPageTemplate="10,25,50" lazy="true"
                                     paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                                     emptyMessage="Não há atores cadastrados">
                            <p:column styleClass="columnToggler">
                                <p:rowToggler/>
                            </p:column>
                            <p:column filterStyle="width: 200px; margin-top:3px;"  filterMatchMode="contains" filterBy="name">
                                <f:facet name="header">
                                    <h:outputText value="Nome"/>
                                </f:facet>
                                <h:outputLink value="../edit/humanEdit.xhtml">
                                    <f:param value="#{actor.person.id}" name="human"/>
                                    <h:outputText value="#{actor.person.name}"/>
                                </h:outputLink>
                            </p:column>
                            <p:column filterStyle="width: 200px; margin-top:3px;" filterMatchMode="contains" filterBy="#{actor.unity.name}">
                                <f:facet name="header">
                                    <h:outputText value="Unidade"/>
                                </f:facet>
                                <h:outputText value="#{actor.unity.name}"/>
                            </p:column>
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Função"/>
                                </f:facet>
                                <h:outputText value="#{actor.personage.name}"/>
                            </p:column>
                            <p:column id="activationColumn">
                                <f:facet name="header">
                                    <h:outputText value="Ativação"/>
                                </f:facet>
                                <h:outputText value="#{actor.activated.time}">
                                    <f:convertDateTime pattern="dd/MM/yyyy"/>
                                </h:outputText>
                            </p:column>
                            <p:column id="deactivationColumn">
                                <f:facet name="header">
                                    <h:outputText value="Desativação"/>
                                </f:facet>
                                <h:outputText value="#{actor.deactivated.time}">
                                    <f:convertDateTime pattern="dd/MM/yyyy"/>
                                </h:outputText>
                            </p:column>
                            <p:rowExpansion>
                                <h:panelGrid columns="2" cellpadding="4">
                                    <h:outputLabel value="Nascimento: " for="birthday"/>
                                    <h:outputText value="#{sessionBean.findHuman(actor.person.id).birthday.time}" id="birthday">
                                        <f:convertDateTime pattern="dd/MM/yyyy"/>
                                    </h:outputText>
                                    <h:outputLabel value="Endereço: " for="address"/>
                                    <h:outputText id="address" value="#{sessionBean.findHuman(actor.person.id).address.street.name}, #{sessionBean.findHuman(actor.person.id).address.number}.
                                                  #{sessionBean.findHuman(actor.person.id).address.complement}. #{sessionBean.findHuman(actor.person.id).address.zipcode.city.name}/#{sessionBean.findHuman(actor.person.id).address.zipcode.city.state.name}"/>
                                    <ui:repeat value="#{unityEdit.personAdditionalInfo(actor.person.id)}" var="addInfo">
                                        <h:outputLabel value="#{addInfo.additionalInfoType.acronym}: "/>
                                        <h:outputText value="#{addInfo.value}"/>
                                    </ui:repeat>
                                </h:panelGrid>
                            </p:rowExpansion>
                            <p:column styleClass="columnEdit">
                                <f:facet name="header">
                                    <h:outputText value="Edição"/>
                                </f:facet>
                                <p:commandButton icon="ui-icon-search" update=":editActorForm, :msg"
                                                 oncomplete="editActor.show();"
                                                 action="#{unityEdit.preEdit()}">
                                    <f:setPropertyActionListener value="#{actor}" target="#{unityEdit.actor}"/>
                                </p:commandButton>
                                <p:commandButton icon="ui-icon-close" ajax="true" update=":removeActorForm"
                                                 oncomplete="confirmExclusion.show();" disabled="#{not unityEdit.dependecyCheck(actor)}">
                                    <f:setPropertyActionListener value="#{actor}" target="#{unityEdit.actor}"/>
                                </p:commandButton>
                            </p:column>
                        </p:dataTable>
                    </p:panel>
                </h:form>
            </p:tab>
        </p:tabView>
    </ui:define>

    <ui:define name="modals">
        <p:dialog modal="true" header="Editar ator" widgetVar="editActor" resizable="false"
                  width="500" height="200">
            <h:form id="editActorForm">
                <p:messages/>
                <h:panelGrid columns="2">
                    <h:outputLabel value="Nome: " for="name"/>
                    <h:outputText id="name" 
                                  value="#{unityEdit.actor.person.name}"/>

                    <h:outputLabel for="activation" value="Ativação: "/>
                    <p:inputMask id="activation" mask="99/99/9999" 
                                 value="#{unityEdit.actor.activated.time}"
                                 required="true" 
                                 requiredMessage="Ativação é obrigatório">
                        <f:convertDateTime pattern="dd/MM/yyyy"/>
                    </p:inputMask>
                    <h:outputLabel for="deactivation" value="Desativação: "/>
                    <p:inputMask id="deactivation" mask="99/99/9999" value="#{unityEdit.deactivation}">
                        <f:convertDateTime pattern="dd/MM/yyyy"/>
                    </p:inputMask>
                    <p:commandButton value="Editar" process="@form"
                                     ajax="true" action="#{unityEdit.editActor()}"
                                     update="@form :unityEditView:actorsForm, :msg"
                                     oncomplete="handleSubmitRequest(xhr, status, args)"/>

                </h:panelGrid>
            </h:form>
        </p:dialog>
        
        <p:dialog modal="true" header="Excluir Aluno" widgetVar="confirmExclusion" resizable="false">
            <h:form id="removeActorForm">
                <table width="100%">
                    <tbody>
                        <tr>
                            <td align="center" width="100%" colspan="2">
                                <br/>
                                <h:outputText value="Você realmente deseja excluir o aluno?"></h:outputText>
                                <br/>
                                <h:outputText value="OBS: Será excluído toda frequência do aluno."></h:outputText>
                                <br/>
                                <br/>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" width="50%">
                                <p:commandButton value="Confirmar" action="#{unityEdit.deleteActor()}" update="@form :unityEditView:actorsForm" onclick="confirmExclusion.hide();"/>
                            </td>
                            <td align="center" width="50%">
                                <p:commandButton value="Cancelar" onclick="confirmExclusion.hide();"/>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </h:form>
        </p:dialog>

        <script type="text/javascript">  
            function handleSubmitRequest(xhr, status, args) {  
                if(!args.validationFailed) {                  
                    editActor.hide();
                }   
            }
        </script>
    </ui:define>
</ui:composition>